 package handling.channel;

 import client.MapleCharacter;
 import client.MapleCharacterUtil;
 import configs.ServerConfig;
 import handling.world.CharacterTransfer;
 import handling.world.CheaterData;
 import handling.world.WorldFindService;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Observable;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import server.Timer;
 import server.commands.PlayerGMRank;
 import server.console.groups.datamanage.PlayerPane;



 public class PlayerStorage
 {
/*  27 */   private final ReentrantReadWriteLock mutex = new ReentrantReadWriteLock();
/*  28 */   private final Lock readLock = this.mutex.readLock(); private final Lock writeLock = this.mutex.writeLock();
/*  29 */   private final ReentrantReadWriteLock mutex2 = new ReentrantReadWriteLock();
/*  30 */   private final Lock connectcheckReadLock = this.mutex2.readLock(); private final Lock pendingWriteLock = this.mutex2.writeLock();
/*  31 */   private final Map<String, MapleCharacter> nameToChar = new LinkedHashMap<>();
/*  32 */   private final Map<Integer, MapleCharacter> idToChar = new LinkedHashMap<>();
/*  33 */   private final Map<Integer, CharacterTransfer> PendingCharacter = new HashMap<>();
/*  34 */   private final PlayerObservable playerObservable = new PlayerObservable();
   private final int channel;

   public PlayerStorage(int channel) {
/*  38 */     this.channel = channel;

/*  40 */     Timer.PingTimer.getInstance().register(new PersistingTask(), 10000L);
/*  41 */     Timer.PingTimer.getInstance().register(new ConnectChecker(), 60000L);
   }

   public ArrayList<MapleCharacter> getAllCharacters() {
/*  45 */     this.readLock.lock();
     try {
/*  47 */       return new ArrayList(this.idToChar.values());
     } finally {
/*  49 */       this.readLock.unlock();
     }
   }




   public void registerPlayer(MapleCharacter chr) {
/*  57 */     this.writeLock.lock();
     try {
/*  59 */       this.nameToChar.put(chr.getName().toLowerCase(), chr);
/*  60 */       this.idToChar.put(Integer.valueOf(chr.getId()), chr);
/*  61 */       this.playerObservable.changed();
     } finally {
/*  63 */       this.writeLock.unlock();
     }

/*  66 */     WorldFindService.getInstance().register(chr.getId(), chr.getAccountID(), chr.getName(), this.channel);
/*  67 */     PlayerPane.getInstance(null).registerIDs(chr.getId(), chr.getPlayerObservable());
   }













   public void registerPendingPlayer(CharacterTransfer chr, int playerId) {
/*  83 */     this.writeLock.lock();
     try {
/*  85 */       this.PendingCharacter.put(Integer.valueOf(playerId), chr);
     } finally {
/*  87 */       this.writeLock.unlock();
     }
   }





   public void deregisterPlayer(MapleCharacter chr) {
/*  96 */     removePlayer(chr.getId(), chr.getName());
/*  97 */     WorldFindService.getInstance().forceDeregister(chr.getId(), chr.getName());
   }





   public void deregisterPlayer(int idz, String namez, int accId) {
/* 105 */     removePlayer(idz, namez);
/* 106 */     WorldFindService.getInstance().forceDeregister(idz, namez, accId);
   }





   public void deregisterPlayer(int idz, String namez) {
/* 114 */     removePlayer(idz, namez);
/* 115 */     WorldFindService.getInstance().forceDeregister(idz, namez);
   }





   public void disconnectPlayer(MapleCharacter chr) {
/* 123 */     removePlayer(chr.getId(), chr.getName());
/* 124 */     WorldFindService.getInstance().forceDeregisterEx(chr.getId(), chr.getName());
   }

   private void removePlayer(int idz, String namez) {
/* 128 */     this.writeLock.lock();
     try {
/* 130 */       this.nameToChar.remove(namez.toLowerCase());
/* 131 */       MapleCharacter chr = this.idToChar.remove(Integer.valueOf(idz));
/* 132 */       if (chr != null) {
/* 133 */         chr.setOnlineTime();
/* 134 */         PlayerPane.getInstance(null).removeIDs(chr.getId(), chr.getPlayerObservable());
       }
/* 136 */       this.playerObservable.changed();
     } finally {
/* 138 */       this.writeLock.unlock();
     }
   }

   public CharacterTransfer getPendingCharacter(int playerId) throws IOException {
/* 143 */     this.writeLock.lock();
     try {
/* 145 */       return this.PendingCharacter.remove(Integer.valueOf(playerId));
     } finally {
/* 147 */       this.writeLock.unlock();
     }
   }

   public MapleCharacter getCharacterByName(String name) {
/* 152 */     this.readLock.lock();
     try {
/* 154 */       return this.nameToChar.get(name.toLowerCase());
     } finally {
/* 156 */       this.readLock.unlock();
     }
   }

   public MapleCharacter getCharacterById(int id) {
/* 161 */     this.readLock.lock();
     try {
/* 163 */       return this.idToChar.get(Integer.valueOf(id));
     } finally {
/* 165 */       this.readLock.unlock();
     }
   }

   public int getConnectedClients() {
/* 170 */     return this.idToChar.size();
   }

   public List<CheaterData> getCheaters() {
/* 174 */     List<CheaterData> cheaters = new ArrayList<>();
/* 175 */     this.readLock.lock();
     try {
/* 177 */       Iterator<MapleCharacter> itr = this.nameToChar.values().iterator();

/* 179 */       while (itr.hasNext()) {
/* 180 */         MapleCharacter chr = itr.next();
/* 181 */         if (chr.getCheatTracker().getPoints() > 0) {
/* 182 */           cheaters.add(new CheaterData(chr.getCheatTracker().getPoints(), MapleCharacterUtil.makeMapleReadable(chr.getName()) + " ID: " + chr.getId() + " (" + chr.getCheatTracker().getPoints() + ") " + chr.getCheatTracker().getSummary()));
         }
       }
     } finally {
/* 186 */       this.readLock.unlock();
     }
/* 188 */     return cheaters;
   }

   public List<CheaterData> getReports() {
/* 192 */     List<CheaterData> cheaters = new ArrayList<>();
/* 193 */     this.readLock.lock();
     try {
/* 195 */       Iterator<MapleCharacter> itr = this.nameToChar.values().iterator();

/* 197 */       while (itr.hasNext()) {
/* 198 */         MapleCharacter chr = itr.next();
/* 199 */         if (chr.getReportPoints() > 0) {
/* 200 */           cheaters.add(new CheaterData(chr.getReportPoints(), MapleCharacterUtil.makeMapleReadable(chr.getName()) + " ID: " + chr.getId() + " (" + chr.getReportPoints() + ") " + chr.getReportSummary()));
         }
       }
     } finally {
/* 204 */       this.readLock.unlock();
     }
/* 206 */     return cheaters;
   }




   public void disconnectAll() {
/* 213 */     disconnectAll(false);
   }




   public void disconnectAll(boolean checkGM) {
/* 220 */     this.writeLock.lock();
     try {
/* 222 */       Iterator<MapleCharacter> chrit = this.nameToChar.values().iterator();

/* 224 */       while (chrit.hasNext()) {
/* 225 */         MapleCharacter chr = chrit.next();
/* 226 */         if (!chr.isGM() || !checkGM) {
/* 227 */           chr.getClient().disconnect(false, false, true);
/* 228 */           if (chr.getClient().getSession().isActive()) {
/* 229 */             chr.getClient().getSession().close();
           }
/* 231 */           WorldFindService.getInstance().forceDeregister(chr.getId(), chr.getName());
/* 232 */           chrit.remove();
         }
       }
     } finally {
/* 236 */       this.writeLock.unlock();
     }
   }




   public String getOnlinePlayers(boolean byGM) {
/* 244 */     StringBuilder sb = new StringBuilder();
/* 245 */     if (byGM) {
/* 246 */       this.readLock.lock();
       try {
/* 248 */         for (MapleCharacter mapleCharacter : this.nameToChar.values()) {
/* 249 */           sb.append(MapleCharacterUtil.makeMapleReadable(mapleCharacter.getName()));
/* 250 */           sb.append(", ");
         }
       } finally {
/* 253 */         this.readLock.unlock();
       }
     } else {
/* 256 */       this.readLock.lock();
       try {
/* 258 */         Iterator<MapleCharacter> itr = this.nameToChar.values().iterator();

/* 260 */         while (itr.hasNext()) {
/* 261 */           MapleCharacter chr = itr.next();
/* 262 */           if (!chr.isGM()) {
/* 263 */             sb.append(MapleCharacterUtil.makeMapleReadable(chr.getName()));
/* 264 */             sb.append(", ");
           }
         }
       } finally {
/* 268 */         this.readLock.unlock();
       }
     }
/* 271 */     return sb.toString();
   }




   public void broadcastPacket(byte[] data) {
/* 278 */     this.readLock.lock();
     try {
/* 280 */       for (MapleCharacter mapleCharacter : this.nameToChar.values()) {
/* 281 */         mapleCharacter.getClient().announce(data);
       }
     } finally {
/* 284 */       this.readLock.unlock();
     }
   }




   public void broadcastSmegaPacket(byte[] data) {
/* 292 */     this.readLock.lock();
     try {
/* 294 */       Iterator<MapleCharacter> itr = this.nameToChar.values().iterator();

/* 296 */       while (itr.hasNext()) {
/* 297 */         MapleCharacter chr = itr.next();
/* 298 */         if (chr.getClient().isLoggedIn() && chr.getSmega()) {
/* 299 */           chr.send(data);
         }
       }
     } finally {
/* 303 */       this.readLock.unlock();
     }
   }




   public void broadcastGMPacket(byte[] data) {
/* 311 */     this.readLock.lock();
     try {
/* 313 */       Iterator<MapleCharacter> itr = this.nameToChar.values().iterator();

/* 315 */       while (itr.hasNext()) {
/* 316 */         MapleCharacter chr = itr.next();
/* 317 */         if (((chr.isAdmin() && ServerConfig.WORLD_SHOWGMPACKET) || chr.getGMLevel() == PlayerGMRank.INTERN.getLevel()) && chr.getClient().isLoggedIn()) {
/* 318 */           chr.send(data);
         }
       }
     } finally {
/* 322 */       this.readLock.unlock();
     }
   }

   public PlayerObservable getPlayerObservable() {
/* 327 */     return this.playerObservable;
   }




















   public class PersistingTask
     implements Runnable
   {
     public void run() {
/* 353 */       PlayerStorage.this.pendingWriteLock.lock();
       try {
/* 355 */         long currenttime = System.currentTimeMillis();


/* 358 */         PlayerStorage.this.PendingCharacter.entrySet().removeIf(next -> (currenttime - ((CharacterTransfer)next.getValue()).TranferTime > 10000L));
       } finally {
/* 360 */         PlayerStorage.this.pendingWriteLock.unlock();
       }
     }
   }

   private class ConnectChecker implements Runnable {
     private ConnectChecker() {}

     public void run() {
/* 369 */       PlayerStorage.this.connectcheckReadLock.lock();
       try {
/* 371 */         Iterator<MapleCharacter> chrit = PlayerStorage.this.nameToChar.values().iterator();
/* 372 */         Map<Integer, MapleCharacter> disconnectList = new LinkedHashMap<>();

/* 374 */         while (chrit.hasNext()) {
/* 375 */           MapleCharacter player = chrit.next();
/* 376 */           if (player != null && !player.getClient().getSession().isActive()) {
/* 377 */             disconnectList.put(Integer.valueOf(player.getId()), player);
           }
         }
/* 380 */         Iterator<MapleCharacter> dcitr = disconnectList.values().iterator();
/* 381 */         while (dcitr.hasNext()) {
/* 382 */           MapleCharacter player = dcitr.next();
/* 383 */           if (player != null) {
/* 384 */             player.getClient().disconnect(false, false);
/* 385 */             player.getClient().updateLoginState(0);
/* 386 */             PlayerStorage.this.disconnectPlayer(player);
/* 387 */             dcitr.remove();
           }
         }
       } finally {
/* 391 */         PlayerStorage.this.connectcheckReadLock.unlock();
       }
     }
   }

   public class PlayerObservable extends Observable {
     private int count;

     public int getCount() {
/* 400 */       return this.count;
     }

     public void changed() {
/* 404 */       this.count = PlayerStorage.this.nameToChar.size();
/* 405 */       setChanged();
/* 406 */       notifyObservers();
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\channel\PlayerStorage.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */